package view;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import connection.Connection;

public class ViewClient extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanel panel;
	private JTextArea text;
	private static JTextArea responseText;
	private JScrollPane scrollingText;
	private JScrollPane scrollingResponseText;
	private JButton button;
	private Connection c = new Connection();
	
	public ViewClient() {
		setTitle("Database Management System");
		setSize(800, 300);
	
		contentPane = new JPanel();
		contentPane.setLayout(new GridLayout());
		
		panel = new JPanel();
		panel.setLayout(new GridLayout(2, 1));
		
		text = new JTextArea();
		scrollingText = new JScrollPane(text);
		text.setBorder(new LineBorder(Color.RED));
		
		responseText = new JTextArea();
		responseText.setEnabled(false);
		responseText.setBorder(new LineBorder(Color.RED));
		scrollingResponseText = new JScrollPane(responseText);
		
		ImageIcon execute = new ImageIcon("img/execute_icon.jpg");
		button = new JButton(execute);
		button.setBackground(Color.WHITE);
		button.setBorder(new LineBorder(Color.RED));
		
		panel.add(scrollingText);
		panel.add(button);
		contentPane.add(panel);
		contentPane.add(scrollingResponseText);
		
		setContentPane(contentPane);
	
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent argEvent) {
				if (text.getText().trim().isEmpty())  
					System.out.println("There is nothing to process!");
				else
					c.send(text.getText());
				text.setText("");
			}
		});
		
		setResizable(true);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public static void setResponse(String text) {
		responseText.append(text + "\n");
	}
}
